<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
        
    <title>php - How to render ZF2 view within JSON response? - Stack Overflow</title>
    <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
    <link rel="apple-touch-icon image_src" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="http://stackoverflow.com/opensearch.xml">
    
    <script src="how-to-render-zf2-view-within-json-response_files/adzerk1_2_4_43adzerk2_2_17_45adzerk3_2_4_44.js" async="" type="text/javascript"></script><script src="how-to-render-zf2-view-within-json-response_files/ga.js" async="" type="text/javascript"></script><script src="how-to-render-zf2-view-within-json-response_files/quant.js" async="" type="text/javascript"></script><script type="text/javascript" src="how-to-render-zf2-view-within-json-response_files/jquery.js"></script>
    <script type="text/javascript" src="how-to-render-zf2-view-within-json-response_files/stub.js"></script>
    <link rel="stylesheet" type="text/css" href="how-to-render-zf2-view-within-json-response_files/all.css">
    
    <meta name="relativepagescore" content="213517">
    <link rel="canonical" href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response">
    <link rel="alternate" type="application/atom+xml" title="Feed for question 'How to render ZF2 view within JSON response?'" href="http://stackoverflow.com/feeds/question/12451399">
    <script type="text/javascript">
        
        StackExchange.ready(function () {
            StackExchange.using("postValidation", function () {
                StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
            });

            
            StackExchange.question.init({showAnswerHelp:true,totalCommentCount:2,shownCommentCount:2,highlightColor:'#F4A83D',backgroundColor:'#FFF',questionId:12451399});

            styleCode();

                StackExchange.realtime.subscribeToQuestion('1', '12451399');
            
                
        });
    </script>


    <script type="text/javascript">
        StackExchange.init({"stackAuthUrl":"https://stackauth.com","serverTime":1367606051,"styleCode":true,"enableUserHovercards":true,"site":{"name":"Stack Overflow","description":"Q&A for professional and enthusiast programmers","isNoticesTabEnabled":true,"recaptchaPublicKey":"6LdchgIAAAAAAJwGpIzRQSOFaO0pU6s44Xt8aTwc","useNewStyleFavoriteTagsEditor":true,"enableSocialMediaInSharePopup":true},"user":{"fkey":"a495a9d53c78cf374b2861f283f8565c","isAnonymous":true}});
        StackExchange.using.setCacheBreakers({"js/prettify-full.js":"6c261bebf56a","js/moderator.js":"f96f7e4ee5dc","js/full-anon.js":"b9443c75283a","js/full.js":"c9131e40e3fb","js/wmd.js":"e62e6bd6c127","js/third-party/jquery.autocomplete.min.js":"e5f01e97f7c3","js/mobile.js":"6eb68240242f","js/help.js":"fc9fb0517db2","js/tageditor.js":"450c9e8426fc","js/tageditornew.js":"b6c68ad4c7dd","js/inline-tag-editing.js":"8e84e8a137f7","js/revisions.js":"7273bb714bba","js/review.js":"56c3941a5bfc","js/tagsuggestions.js":"aa48ef6154df","js/post-validation.js":"98fa812e7e1a","js/explore-qlist.js":"44794e78c6be"});
        
    </script>
        <script type="text/javascript">
        StackExchange.using("gps", function() {
             StackExchange.gps.init(true);
        });
    </script>
    
        <script type="text/javascript">
            StackExchange.ready(function () {
                $('#nav-tour').click(function () {
                    StackExchange.using("gps", function() {
                        StackExchange.gps.track("aboutpage.click", { aboutclick_location: "headermain" }, true);
                    });
                });
            });
        </script>
<script src="how-to-render-zf2-view-within-json-response_files/full-anon.js" type="text/javascript" async=""></script><script src="how-to-render-zf2-view-within-json-response_files/post-validation.js" type="text/javascript" async=""></script><script src="how-to-render-zf2-view-within-json-response_files/prettify-full.js" type="text/javascript" async=""></script><script src="how-to-render-zf2-view-within-json-response_files/adFeedback.js" type="text/javascript"></script><link href="how-to-render-zf2-view-within-json-response_files/adFeedback.css" rel="stylesheet"><link rel="stylesheet" type="text/css" href="how-to-render-zf2-view-within-json-response_files/sidebar.css"></head>
<body class="question-page">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <div id="overlay-header"></div>
    <div id="custom-header"></div>

    <div class="container">
        <div id="header" class="headeranon">
            <div id="portalLink">
                <a class="genu" onclick="StackExchange.ready(function(){genuwine.click();});return false;">Stack Exchange</a>
            </div>
            <div id="topbar">
                <div id="hlinks">
                    
<span id="hlinks-user"></span>
<span id="hlinks-nav">                        <a href="http://stackoverflow.com/users/login?returnurl=%2fquestions%2f12451399%2fhow-to-render-zf2-view-within-json-response">sign up</a>

 <span class="lsep">|</span>
                    <a href="http://stackoverflow.com/users/login?returnurl=%2fquestions%2f12451399%2fhow-to-render-zf2-view-within-json-response">log in</a>

 <span class="lsep">|</span>
                    <a href="http://careers.stackoverflow.com/">careers 2.0</a>

 <span class="lsep">|</span>
</span>
<span id="hlinks-custom"></span>
                </div>
                <div id="hsearch">
                    <form id="search" action="/search" method="get" autocomplete="off">
                        <div>
                            <input autocomplete="off" name="q" class="textbox" placeholder="search" tabindex="1" maxlength="240" size="28" type="text">
                        </div>
                    </form>
                </div>
            </div>
            <br class="cbt">
            <div id="hlogo">
                <a href="http://stackoverflow.com/">
                    Stack Overflow
                </a>
            </div>
            <div id="hmenus">
                <div class="nav mainnavs mainnavsanon">
                    <ul>
                            <li class="youarehere"><a id="nav-questions" href="http://stackoverflow.com/questions">Questions</a></li>
                            <li><a id="nav-tags" href="http://stackoverflow.com/tags">Tags</a></li>
                            <li><a id="nav-tour" href="http://stackoverflow.com/about">Tour</a></li>
                            <li><a id="nav-users" href="http://stackoverflow.com/users">Users</a></li>
                    </ul>
                </div>
                <div class="nav askquestion">
                    <ul>
                        <li>
                            <a id="nav-askquestion" href="http://stackoverflow.com/questions/ask">Ask Question</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        



        <div id="content">
            

<div itemscope="" itemtype="http://schema.org/Article">
<link itemprop="image" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
<div id="herobox-mini">
    <div id="hero-content">
        <span id="controls">
            <a href="http://stackoverflow.com/about" id="tell-me-more" class="button">Tell me more</a>
            <span id="close"><a title="click to dismiss">×</a></span>
        </span>
        <div id="blurb">
            <span id="site-name">Stack Overflow</span> is a question and answer site for 
            professional and enthusiast programmers. It's 100% free, no registration required.
        </div>        
    </div>
    <script>
        $('#tell-me-more').click(function () {
            var clickSource = $("body").attr("class") + '-mini';
            if ($("body").hasClass("questions-page")) {
                clickSource = 'questionpagemini';
            }
            if ($("body").hasClass("home-page")) {
                clickSource = 'homepagemini';
            }

            StackExchange.using("gps", function () {
                StackExchange.gps.track("aboutpage.click", { aboutclick_location: clickSource } , true);
            });
        });
        $('#herobox-mini #close').click(function () {
            $.cookie("hero", "none", { path: "/" });
            var $hero = $("#herobox-mini");
            $hero.slideUp('fast', function () { $hero.remove(); });
            return false;
        });
    </script>
</div>
<div id="question-header">
    <h1 itemprop="name"><a href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response" class="question-hyperlink">How to render ZF2 view within JSON response?</a></h1>
</div>
<div id="mainbar">



<div class="question" data-questionid="12451399" id="question">
    
            <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk1"><a href="http://engine.adzerk.net/r?e=eyJ0cyI6MTM2NzYwNjA0NDI5NiwiYXYiOjQxNCwiYXQiOjQsImNtIjo4NDcsImNoIjoxMTc4LCJjciI6NTA5NCwiZG0iOjEsImZjIjoxMDEzOSwiZmwiOjI0NDQsImt3IjoicGhwLGpzb24semVuZC1mcmFtZXdvcmsyLHBocC01LjQsemVuZC1mcmFtZXdvcmstbXZjIiwibnciOjIyLCJyZiI6Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS91cmw_c2E9dCIsInJ2IjowLCJwciI6MTU2OCwic3QiOjgyNzcsInpuIjo0MywiZGkiOiJhMjFjZGQ2MmIxNzU0NWQ0YjQ2MmQ0OWFmM2RkODVhMCIsInVyIjoiaHR0cDovL2NhcmVlcnMuc3RhY2tvdmVyZmxvdy5jb20vP2JtaWQ9QzIwQjEgIn0&amp;s=t9UrYesvb6rVevd3KR16BLgS5mE" rel="nofollow" target="_blank" title=""><img src="how-to-render-zf2-view-within-json-response_files/3478c54721cd466fb6f7d3afe16e97d4.gif" title="" alt="" border="0" height="90" width="728"></a><img src="how-to-render-zf2-view-within-json-response_files/i_002.gif" border="0" height="0px" width="0px"><div class="adzerk-vote-controls" style="display: none;"><div class="adzerk-vote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></div><div class="adzerk-vote-survey" style="display:none;"><form><span>No problem. We won't show you that ad again. Why didn't you like it?</span><ul><li><label><input value="12" name="downvoteReason" type="radio">Uninteresting</label></li><li><label><input value="13" name="downvoteReason" type="radio">Misleading</label></li><li><label><input value="14" name="downvoteReason" type="radio">Offensive</label></li><li><label><input value="15" name="downvoteReason" type="radio">Repetitive</label></li></ul><a href="#" class="adzerk-vote-cancel">Oops! I didn't mean to do this.</a></form></div></div>


    <table>
        <tbody><tr>
            <td class="votecell">
                

<div class="vote">
    <input value="12451399" type="hidden">
    <a class="vote-up-off" title="This question shows research effort; it is useful and clear (click again to undo)">up vote</a>
    <span class="vote-count-post ">4</span>
    <a class="vote-down-off" title="This question does not show any research effort; it is unclear or not useful (click again to undo)">down vote</a>
    
    <a class="star-off" href="#" title="This is a favorite question (click again to undo)">favorite</a>
    <div class="favoritecount"><b>4</b></div>   

 
</div>

            </td>
            
<td class="postcell">
<div>
    <div class="post-text" itemprop="description">
        <p>So far, I have figured out how to return a typical JSON response in Zend Framework 2.  First, I added the <code>ViewJsonStrategy</code> to the <code>strategies</code> section of the <code>view_manager</code> configuration.  Then, instead of returning a <code>ViewModel</code> instance from the controller action, I return a <code>JsonModel</code> instance with all my variables set.</p>

<p>Now that I've figured that piece out, I need to understand how to 
render a view and return it within that JSON response.  In ZF1, I was 
able to use <code>$this-&gt;view-&gt;render($scriptName)</code>, which returned the HTML as a string.  In ZF2, the <code>Zend\View\View::render(...)</code> method returns <code>void</code>.</p>

<p>So... how can I render an HTML view script and return it in a JSON response in one request?</p>

<p>This is what I have right now:</p>



<pre style="" class="lang-php prettyprint prettyprinted"><code><span class="pln">    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$this</span><span class="pun">-&gt;</span><span class="pln">getRequest</span><span class="pun">()-&gt;</span><span class="pln">isXmlHttpRequest</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $jsonModel </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">JsonModel</span><span class="pun">(...);</span><span class="pln">

        </span><span class="com">/* @todo Render HTML script into `$html` variable, and add to `JsonModel` */</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> $jsonModel</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ViewModel</span><span class="pun">(...);</span><span class="pln">
    </span><span class="pun">}</span></code></pre>

    </div>
    <div class="post-taglist">
        <a href="http://stackoverflow.com/questions/tagged/php" class="post-tag" title="show questions tagged 'php'" rel="tag">php</a> <a href="http://stackoverflow.com/questions/tagged/json" class="post-tag" title="show questions tagged 'json'" rel="tag">json</a> <a href="http://stackoverflow.com/questions/tagged/zend-framework2" class="post-tag" title="show questions tagged 'zend-framework2'" rel="tag">zend-framework2</a> <a href="http://stackoverflow.com/questions/tagged/php-5.4" class="post-tag" title="show questions tagged 'php-5.4'" rel="tag">php-5.4</a> <a href="http://stackoverflow.com/questions/tagged/zend-framework-mvc" class="post-tag" title="show questions tagged 'zend-framework-mvc'" rel="tag">zend-framework-mvc</a> 
    </div>
    <table class="fw">
    <tbody><tr>
    <td class="vt">









<div class="post-menu"><a href="http://stackoverflow.com/q/12451399" title="short permalink to this question" class="short-link" id="link-post-12451399">share</a><span class="lsep">|</span><a href="http://stackoverflow.com/posts/12451399/edit" class="suggest-edit-post" title="">improve this question</a></div>            </td>
    <td class="post-signature" align="right">


    <div class="user-info ">
        <div class="user-action-time">
                            edited
                             <a href="http://stackoverflow.com/posts/12451399/revisions" title="show all edits to this post"><span title="2012-09-18 04:14:03Z" class="relativetime">Sep 18 '12 at 4:14</span></a>

        </div>
        <div class="user-gravatar32">
            
        </div>
        <div class="user-details">
            <br>
            
        </div>
    </div>
    </td>
    <td class="post-signature owner">
        

    <div class="user-info ">
        <div class="user-action-time">
                                    asked
 <span title="2012-09-16 22:54:18Z" class="relativetime">Sep 16 '12 at 22:54</span>
        </div>
        <div class="user-gravatar32">
            <a href="http://stackoverflow.com/users/281553/webjawns-com"><div class=""><img src="how-to-render-zf2-view-within-json-response_files/c1e937552258a4d499aadf181c87af4b.jpeg" alt="" height="32" width="32"></div></a>
        </div>
        <div class="user-details">
            <a href="http://stackoverflow.com/users/281553/webjawns-com">webjawns.com</a><br>
            <span class="reputation-score" title="reputation score" dir="ltr">1,020</span><span title="5 silver badges"><span class="badge2"></span><span class="badgecount">5</span></span><span title="22 bronze badges"><span class="badge3"></span><span class="badgecount">22</span></span>
        </div>
    </div>

    </td>
    </tr>
    </tbody></table>
</div>
</td>
        </tr>


<tr>
<td class="votecell"></td>
<td>
    <div id="comments-12451399" class="comments">
        <table>
        <tbody>
                    
    <tr id="comment-16869016" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">Hey I'm
 just returning to it since i'm at it, too. What exactly is your problem
 though? When you return a jsonModel(array($data)), then the response is
 json. You do not want to render any view or whatsoever when returning 
json Oo</span> –&nbsp;<a href="http://stackoverflow.com/users/958642/sam" title="5388 reputation" class="comment-user">Sam</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(16869016, 0)" href="#comment16869016_12451399"><span title="2012-09-21 11:40:38Z" class="relativetime-clean">Sep 21 '12 at 11:40</span></a></span></div></td>
    </tr>
            
    <tr id="comment-17062223" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">I need to render a partial view inside the JSON response.  Example: <code>{"html":"&lt;tr&gt;&lt;td&gt;I'm an HTML response&lt;/td&gt;&lt;/tr&gt;"}</code>.</span> –&nbsp;<a href="http://stackoverflow.com/users/281553/webjawns-com" title="1020 reputation" class="comment-user owner">webjawns.com</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17062223, 0)" href="#comment17062223_12451399"><span title="2012-09-29 01:42:58Z" class="relativetime-clean">Sep 29 '12 at 1:42</span></a></span></div></td>
    </tr>

        </tbody>
    
        </table>
    </div>
    
</td>
</tr>            </tbody></table>    
</div>


<div id="answers">

    <a name="tab-top"></a>
    <div id="answers-header">
        <div class="subheader answers-subheader">
            <h2>
                    2 Answers
            </h2>
            <div id="tabs">
                <a href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response?answertab=active#tab-top" title="Answers with the latest activity first">active</a>
<a href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response?answertab=oldest#tab-top" title="Answers in the order they were provided">oldest</a>
<a class="youarehere" href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response?answertab=votes#tab-top" title="Answers with the highest score first">votes</a>

            </div>
        </div>    
    </div>    




  
<a name="12651250"></a>
<div id="answer-12651250" class="answer accepted-answer" data-answerid="12651250">
    <table>
        <tbody><tr>
            <td class="votecell">
                

<div class="vote">
    <input value="12651250" type="hidden">
    <a class="vote-up-off" title="This answer is useful (click again to undo)">up vote</a>
    <span class="vote-count-post ">14</span>
    <a class="vote-down-off" title="This answer is not useful (click again to undo)">down vote</a>
    

            <span class="vote-accepted-on load-accepted-answer-date" title="loading when this answer was accepted...">accepted</span>
<div class="bounty-award-container"><span class="bounty-award" title="This answer has been awarded bounty worth 50 reputation by webjawns.com">+50</span></div> 
</div>

            </td>
            


<td class="answercell">
    <div class="post-text"><p>OK, i think i finally understood what 
you're doing. I've found a solution that i think matches your criteria. 
Though i am sure that there is room for improvement, as there's some 
nasty handwork to be done...</p>

<pre style="" class="lang-php prettyprint prettyprinted"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> indexAction</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(!</span><span class="pln">$this</span><span class="pun">-&gt;</span><span class="pln">getRequest</span><span class="pun">()-&gt;</span><span class="pln">isXmlHttpRequest</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> array</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  $htmlViewPart </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ViewModel</span><span class="pun">();</span><span class="pln">
  $htmlViewPart</span><span class="pun">-&gt;</span><span class="pln">setTerminal</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">)</span><span class="pln">
               </span><span class="pun">-&gt;</span><span class="pln">setTemplate</span><span class="pun">(</span><span class="str">'model/controller/action'</span><span class="pun">)</span><span class="pln">
               </span><span class="pun">-&gt;</span><span class="pln">setVariables</span><span class="pun">(</span><span class="pln">array</span><span class="pun">(</span><span class="pln">
                  </span><span class="str">'key'</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">'value'</span><span class="pln">
               </span><span class="pun">));</span><span class="pln">

  $htmlOutput </span><span class="pun">=</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">getServiceLocator</span><span class="pun">()</span><span class="pln">
                     </span><span class="pun">-&gt;</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'viewrenderer'</span><span class="pun">)</span><span class="pln">
                     </span><span class="pun">-&gt;</span><span class="pln">render</span><span class="pun">(</span><span class="pln">$htmlViewPart</span><span class="pun">);</span><span class="pln">

  $jsonModel </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">JsonModel</span><span class="pun">();</span><span class="pln">
  $jsonModel</span><span class="pun">-&gt;</span><span class="pln">setVariables</span><span class="pun">(</span><span class="pln">array</span><span class="pun">(</span><span class="pln">
    </span><span class="str">'html'</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> $htmlOutput</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'jsonVar1'</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">'jsonVal2'</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'jsonArray'</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> array</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="lit">5</span><span class="pun">,</span><span class="lit">6</span><span class="pun">)</span><span class="pln">
  </span><span class="pun">));</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> $jsonModel</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code></pre>

<p><strike>As you can see, the templateMap i create is ... nasty ... 
it's annoying and i'm sure it can be improved by quite a bit. It's a 
working solution but just not a clean one. Maybe somehow one would be 
able to grab the, probably already instantiated, default PhpRenderer 
from the ServiceLocator with it's template- and path-mapping and then it
 should be cleaner.</strike></p>

<p>Thanks to the comment ot @DrBeza the work needed to be done could be 
reduced by a fair amount. Now, as I'd initially wanted, we will grab the
 viewrenderer with all the template mapping intact and simply render the
 ViewModel directly. The only important factor is that you need to 
specify the fully qualified template to render (e.g.: 
"$module/$controller/$action")</p>

<p>I hope this will get you started though ;)</p>

<p>PS: Response looks like this:</p>

<pre style="" class="lang-php prettyprint prettyprinted"><code><span class="typ">Object</span><span class="pun">:</span><span class="pln">
    html</span><span class="pun">:</span><span class="pln"> </span><span class="str">"&lt;h1&gt;Hello World&lt;/h1&gt;"</span><span class="pln">
    jsonArray</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Array</span><span class="pun">[</span><span class="lit">6</span><span class="pun">]</span><span class="pln">
    jsonVar1</span><span class="pun">:</span><span class="pln"> </span><span class="str">"jsonVal2"</span></code></pre>
</div>
    <table class="fw">
    <tbody><tr>
    <td class="vt">









<div class="post-menu"><a href="http://stackoverflow.com/a/12651250" title="short permalink to this answer" class="short-link" id="link-post-12651250">share</a><span class="lsep">|</span><a href="http://stackoverflow.com/posts/12651250/edit" class="suggest-edit-post" title="">improve this answer</a></div>                    </td>
    <td class="post-signature" align="right">


    <div class="user-info ">
        <div class="user-action-time">
                            edited
                             <a href="http://stackoverflow.com/posts/12651250/revisions" title="show all edits to this post"><span title="2012-10-02 06:24:47Z" class="relativetime">Oct 2 '12 at 6:24</span></a>

        </div>
        <div class="user-gravatar32">
            
        </div>
        <div class="user-details">
            <br>
            
        </div>
    </div>
    </td>
            


    <td class="post-signature" align="right">   
       

    

    <div class="user-info user-hover">
        <div class="user-action-time">
                                    answered
 <span title="2012-09-29 08:39:26Z" class="relativetime">Sep 29 '12 at 8:39</span>
        </div>
        <div class="user-gravatar32">
            <a href="http://stackoverflow.com/users/958642/sam"><div class=""><img src="how-to-render-zf2-view-within-json-response_files/b6fdce589f9a5243a48d541d7444900c.jpeg" alt="" height="32" width="32"></div></a>
        </div>
        <div class="user-details">
            <a href="http://stackoverflow.com/users/958642/sam">Sam</a><br>
            <span class="reputation-score" title="reputation score" dir="ltr">5,388</span><span title="2 gold badges"><span class="badge1"></span><span class="badgecount">2</span></span><span title="6 silver badges"><span class="badge2"></span><span class="badgecount">6</span></span><span title="36 bronze badges"><span class="badge3"></span><span class="badgecount">36</span></span>
        </div>
    </div>

    </td>
    </tr>
    </tbody></table>
</td>
        </tr>



<tr>
<td class="votecell"></td>
<td>
    <div id="comments-12651250" class="comments">
        <table>
        <tbody>
                    
    <tr id="comment-17091966" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">I 
appreciate the response, but I feel like a broken record.  This is 
helpful, but it doesn't answer my question.  I need to return the HTML 
in a JSON response, which will include other variables -- potentially 
multiple views.</span> –&nbsp;<a href="http://stackoverflow.com/users/281553/webjawns-com" title="1020 reputation" class="comment-user owner">webjawns.com</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17091966, 12651250)" href="#comment17091966_12651250"><span title="2012-10-01 02:20:11Z" class="relativetime-clean">Oct 1 '12 at 2:20</span></a></span></div></td>
    </tr>
            
    <tr id="comment-17095197" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">@webjawns.com I've editted my answer, i hope this will get you started.</span> –&nbsp;<a href="http://stackoverflow.com/users/958642/sam" title="5388 reputation" class="comment-user">Sam</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17095197, 12651250)" href="#comment17095197_12651250"><span title="2012-10-01 07:10:54Z" class="relativetime-clean">Oct 1 '12 at 7:10</span></a></span></div></td>
    </tr>
            
    <tr id="comment-17095229" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">The isXmlHttpRequest() can be ignored btw, was just for me to render my template from which i call the ajax-request ;)</span> –&nbsp;<a href="http://stackoverflow.com/users/958642/sam" title="5388 reputation" class="comment-user">Sam</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17095229, 12651250)" href="#comment17095229_12651250"><span title="2012-10-01 07:13:16Z" class="relativetime-clean">Oct 1 '12 at 7:13</span></a></span></div></td>
    </tr>
            
    <tr id="comment-17117409" class="comment">
        <td class="comment-actions"><table><tbody><tr>
<td class="comment-score">
<span title="number of 'useful comment' votes received" class="cool">1</span>
</td>
<td>
&nbsp;
</td></tr>
</tbody></table></td>
        <td class="comment-text"><div><span class="comment-copy">@Sam 
You can shortcut all of that with: $htmlOutput = 
$this-&gt;getServiceLocator()-&gt;get('viewrenderer')-&gt;render('test/test',
 $viewModel).</span> –&nbsp;<a href="http://stackoverflow.com/users/1022873/drbeza" title="1336 reputation" class="comment-user">DrBeza</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17117409, 12651250)" href="#comment17117409_12651250"><span title="2012-10-01 23:18:11Z" class="relativetime-clean">Oct 1 '12 at 23:18</span></a></span><span class="edited-yes" title="this comment was edited"></span></div></td>
    </tr>
            
    <tr id="comment-17121509" class="comment">
        <td class="comment-actions"><table><tbody><tr>
<td class="comment-score">
<span title="number of 'useful comment' votes received" class="cool">1</span>
</td>
<td>
&nbsp;
</td></tr>
</tbody></table></td>
        <td class="comment-text"><div><span class="comment-copy">@DrBeza
 Thanks for clarifying, i thought that it'd be possible to do, but 
simple didn't know how to :) I'll try and modify the answer :)</span> –&nbsp;<a href="http://stackoverflow.com/users/958642/sam" title="5388 reputation" class="comment-user">Sam</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17121509, 12651250)" href="#comment17121509_12651250"><span title="2012-10-02 05:49:29Z" class="relativetime-clean">Oct 2 '12 at 5:49</span></a></span></div></td>
    </tr>

        </tbody>
    
        </table>
    </div>
    <a id="comments-link-12651250" class="comments-link" data-comments-count="2" title="expand to show all comments on this post">show <b>2</b> more comments</a>
</td>
</tr>
   
    </tbody></table>
</div>
        <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk3"><a href="http://engine.adzerk.net/r?e=eyJ0cyI6MTM2NzYwNjA0NDI5OSwiYXYiOjQxNCwiYXQiOjQsImNtIjoyOTExLCJjaCI6MTE3OCwiY3IiOjU5MTYsImRtIjoxLCJmYyI6MTAxODEsImZsIjo3NDcyLCJrdyI6InBocCxqc29uLHplbmQtZnJhbWV3b3JrMixwaHAtNS40LHplbmQtZnJhbWV3b3JrLW12YyIsIm53IjoyMiwicmYiOiJodHRwOi8vd3d3Lmdvb2dsZS5jb20vdXJsP3NhPXQiLCJydiI6MCwicHIiOjE2MDQsInN0Ijo4Mjc3LCJ6biI6NDQsImRpIjoiMDQ5NmE2OTNhM2Y1NDI1MGJmOTk5ZDk3NmI2MmNkMzEiLCJ1ciI6Imh0dHA6Ly9jYXJlZXJzLnN0YWNrb3ZlcmZsb3cuY29tLyJ9&amp;s=WHC1hp5F_5VoVeB8EuKwVyPBKDM" rel="nofollow" target="_blank" title=""><img src="how-to-render-zf2-view-within-json-response_files/d31c8d7d3cf640a6b29d9ad71788dd4a.png" title="" alt="" border="0" height="90" width="728"></a><img src="how-to-render-zf2-view-within-json-response_files/i.gif" border="0" height="0px" width="0px"><div class="adzerk-vote-controls" style="display:none;"><div class="adzerk-vote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></div><div class="adzerk-vote-survey" style="display:none;"><form><span>No problem. We won't show you that ad again. Why didn't you like it?</span><ul><li><label><input value="12" name="downvoteReason" type="radio">Uninteresting</label></li><li><label><input value="13" name="downvoteReason" type="radio">Misleading</label></li><li><label><input value="14" name="downvoteReason" type="radio">Offensive</label></li><li><label><input value="15" name="downvoteReason" type="radio">Repetitive</label></li></ul><a href="#" class="adzerk-vote-cancel">Oops! I didn't mean to do this.</a></form></div></div>



  
<a name="12594333"></a>
<div id="answer-12594333" class="answer" data-answerid="12594333">
    <table>
        <tbody><tr>
            <td class="votecell">
                

<div class="vote">
    <input value="12594333" type="hidden">
    <a class="vote-up-off" title="This answer is useful (click again to undo)">up vote</a>
    <span class="vote-count-post ">2</span>
    <a class="vote-down-off" title="This answer is not useful (click again to undo)">down vote</a>
    

 
</div>

            </td>
            


<td class="answercell">
    <div class="post-text"><p>As usual framework developer mess thing about AJAX following the rule why  simple if might be complex Here is simple solution
in controller script</p>

<pre style="" class="lang-php prettyprint prettyprinted"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> checkloginAction</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
   </span><span class="com">// some hosts need to this some not </span><span class="pln">
   </span><span class="com">//header ("Content-type: application/json");  // this work</span><span class="pln">
   </span><span class="com">// prepare json aray ....</span><span class="pln">
   $arr </span><span class="pun">=</span><span class="pln"> $array</span><span class="pun">(</span><span class="str">"some"</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="pun">.....);</span><span class="pln">
   echo json_encode</span><span class="pun">(</span><span class="pln">$arr</span><span class="pun">);</span><span class="pln"> </span><span class="com">// this works</span><span class="pln">
   </span><span class="kwd">exit</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code></pre>

<p>This works in ZF1 and ZF2 as well
No need of view scrpt at all</p>

<p>If you use advise of ZF2 creator</p>

<pre style="" class="lang-php prettyprint prettyprinted"><code><span class="kwd">use</span><span class="pln"> </span><span class="typ">Zend</span><span class="pln">\View\Model\JsonModel</span><span class="pun">;</span><span class="pln">
</span><span class="pun">....</span><span class="pln">


 $result </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">JsonModel</span><span class="pun">(</span><span class="pln">$arr</span><span class="pun">);</span><span class="pln">
 </span><span class="kwd">return</span><span class="pln"> $result</span><span class="pun">;</span></code></pre>

<p>AJAX got null as response at least in zf 2.0.0</p>
</div>
    <table class="fw">
    <tbody><tr>
    <td class="vt">









<div class="post-menu"><a href="http://stackoverflow.com/a/12594333" title="short permalink to this answer" class="short-link" id="link-post-12594333">share</a><span class="lsep">|</span><a href="http://stackoverflow.com/posts/12594333/edit" class="suggest-edit-post" title="">improve this answer</a></div>                    </td>
            


    <td class="post-signature" align="right">   
       

    

    <div class="user-info ">
        <div class="user-action-time">
                                    answered
 <span title="2012-09-26 03:56:01Z" class="relativetime">Sep 26 '12 at 3:56</span>
        </div>
        <div class="user-gravatar32">
            <a href="http://stackoverflow.com/users/1699032/andrey-gache"><div class=""><img src="how-to-render-zf2-view-within-json-response_files/0b3c0ab44dc5c90966ece3044320ae71.png" alt="" height="32" width="32"></div></a>
        </div>
        <div class="user-details">
            <a href="http://stackoverflow.com/users/1699032/andrey-gache">Andrey Gache</a><br>
            <span class="reputation-score" title="reputation score" dir="ltr">21</span><span title="1 bronze badge"><span class="badge3"></span><span class="badgecount">1</span></span>
        </div>
    </div>

    </td>
    </tr>
    </tbody></table>
</td>
        </tr>



<tr>
<td class="votecell"></td>
<td>
    <div id="comments-12594333" class="comments">
        <table>
        <tbody>
                    
    <tr id="comment-17062188" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">I <i>need</i> to render a view script within the Ajax response.</span> –&nbsp;<a href="http://stackoverflow.com/users/281553/webjawns-com" title="1020 reputation" class="comment-user owner">webjawns.com</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17062188, 12594333)" href="#comment17062188_12594333"><span title="2012-09-29 01:38:41Z" class="relativetime-clean">Sep 29 '12 at 1:38</span></a></span></div></td>
    </tr>
            
    <tr id="comment-17062240" class="comment">
        <td></td>
        <td class="comment-text"><div><span class="comment-copy">Like this: <code>{"html":"&lt;strong&gt;I'm an HTML response&lt;/strong&gt;"}</code>.</span> –&nbsp;<a href="http://stackoverflow.com/users/281553/webjawns-com" title="1020 reputation" class="comment-user owner">webjawns.com</a> <span class="comment-date" dir="ltr"><a class="comment-link" onclick="StackExchange.question.highlightComment(17062240, 12594333)" href="#comment17062240_12594333"><span title="2012-09-29 01:44:07Z" class="relativetime-clean">Sep 29 '12 at 1:44</span></a></span></div></td>
    </tr>

        </tbody>
    
        </table>
    </div>
    
</td>
</tr>
   
    </tbody></table>
</div>
    <a name="new-answer"></a>
    <form id="post-form" action="/questions/12451399/answer/submit" method="post" class="post-form">
        <input id="post-id" value="12451399" type="hidden">
        <h2 class="space">Your Answer</h2>



    
<script type="text/javascript">
    StackExchange.ready(function() {
        initTagRenderer("".split(" "), "".split(" "));
         
        prepareEditor({
            heartbeatType: 'answer',
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
        });
        

    });  
</script>


<div id="post-editor" class="post-editor">

    <div style="position: relative;">     
        <div class="wmd-container">
            <div id="wmd-button-bar" class="wmd-button-bar"><ul id="wmd-button-row" class="wmd-button-row"><li style="left: 0px;" class="wmd-button" id="wmd-bold-button"><span style="background-position: 0px -20px;"></span></li><li style="left: 25px;" class="wmd-button" id="wmd-italic-button"><span style="background-position: -20px -20px;"></span></li><li style="left: 50px;" class="wmd-spacer" id="wmd-spacer1"><span style="background-position: -40px -20px;"></span></li><li style="left: 75px;" class="wmd-button" id="wmd-link-button"><span style="background-position: -40px -20px;"></span></li><li style="left: 100px;" class="wmd-button" id="wmd-quote-button"><span style="background-position: -60px -20px;"></span></li><li style="left: 125px;" class="wmd-button" id="wmd-code-button"><span style="background-position: -80px -20px;"></span></li><li style="left: 150px;" class="wmd-button" id="wmd-image-button"><span style="background-position: -100px -20px;"></span></li><li style="left: 175px;" class="wmd-spacer" id="wmd-spacer2"><span style="background-position: -120px -20px;"></span></li><li style="left: 200px;" class="wmd-button" id="wmd-olist-button"><span style="background-position: -120px -20px;"></span></li><li style="left: 225px;" class="wmd-button" id="wmd-ulist-button"><span style="background-position: -140px -20px;"></span></li><li style="left: 250px;" class="wmd-button" id="wmd-heading-button"><span style="background-position: -160px -20px;"></span></li><li style="left: 275px;" class="wmd-button" id="wmd-hr-button"><span style="background-position: -180px -20px;"></span></li><li style="left: 300px;" class="wmd-spacer" id="wmd-spacer3"><span style="background-position: -200px -20px;"></span></li><li style="left: 325px;" class="wmd-button" id="wmd-undo-button"><span style="background-position: -200px -20px;"></span></li><li style="left: 350px;" class="wmd-button" id="wmd-redo-button"><span style="background-position: -220px -20px;"></span></li></ul></div>
            <textarea id="wmd-input" class="wmd-input" name="post-text" cols="92" rows="15" tabindex="101" data-min-length=""></textarea>
        </div>
    </div>

    <div class="fl" style="margin-top: 8px; height:24px;">&nbsp;</div>
    <div id="draft-saved" class="draft-saved community-option fl" style="margin-top: 8px; height:24px; display:none;">draft saved</div>

    <div id="draft-discarded" class="draft-discarded community-option fl" style="margin-top: 8px; height:24px; display:none;">draft discarded</div>



    <div id="wmd-preview" class="wmd-preview"></div>
    <div></div>
    <div class="edit-block">
        <input id="fkey" name="fkey" value="a495a9d53c78cf374b2861f283f8565c" type="hidden">
        <input id="author" name="author" type="text">
    </div>
</div>
        <div style="position: relative;"> 
            <div class="form-item new-post-login">
        
                <div class="new-login-form">
                    <div class="new-login-left">
                        <h3>Sign up or <a id="login-link" href="http://stackoverflow.com/users/login?returnurl=%2fquestions%2f12451399%2fhow-to-render-zf2-view-within-json-response%23new-answer">log in</a></h3>
                        <script type="text/javascript">
                            StackExchange.ready(function () {
                                StackExchange.helpers.onClickDraftSave('#login-link');
                            });
                        </script>
                        <div class="preferred-login google-login">
                            <p><span class="icon"></span><span>Sign up using Google</span></p>
                        </div>
                        <div class="preferred-login facebook-login">
                            <p><span class="icon"></span><span>Sign up using Facebook</span></p>
                        </div>
                        <div class="preferred-login stackexchange-login">
                            <p><span class="icon"></span><span>Sign up using Stack Exchange</span></p>
                        </div>
                    </div>
                    <input name="manual-openid" class="manual-openid" type="hidden">
                    <input name="use-facebook" class="use-facebook" value="false" type="hidden">
                    <input class="submit-openid" value="Submit" style="display:none" type="button">
                    <div class="new-login-right">
                                <h3>Post as a guest</h3>
    <div class="form-item">
        <table>
        <tbody><tr>
                    <script type="text/javascript">
                StackExchange.ready(function () {
                    StackExchange.helpers.bindHelpOverlayEvents($('.vm input'));
                });
            </script>
            <td class="vm">
                <div>
                    <label for="display-name">Name</label>
                    <input id="display-name" name="display-name" size="30" maxlength="30" tabindex="105" type="text">
                </div>
                <div>
                    <label for="m-address">Email</label>
                    <input id="m-address" name="m-address" size="30" maxlength="100" tabindex="106" type="text">
                    <span class="edit-field-overlay">required, but not shown</span>
                </div>
            </td>
        </tr>
        </tbody></table>
    </div>

                    </div>
                </div>
            </div>
            <script type="text/javascript">
                StackExchange.ready(
                    function () {
                        StackExchange.openid.initPostLogin('.new-post-login', '%2fquestions%2f12451399%2fhow-to-render-zf2-view-within-json-response%23new-answer');
                    }
                );
            </script>
            <noscript>
                        <h3>Post as a guest</h3>
    <div class="form-item">
        <table>
        <tr>
                    <script type="text/javascript">
                StackExchange.ready(function () {
                    StackExchange.helpers.bindHelpOverlayEvents($('.vm input'));
                });
            </script>
            <td class="vm">
                <div>
                    <label for="display-name">Name</label>
                    <input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105">
                </div>
                <div>
                    <label for="m-address">Email</label>
                    <input id="m-address" name="m-address" type="text" size="30" maxlength="100" value="" tabindex="106">
                    <span class="edit-field-overlay">required, but not shown</span>
                </div>
            </td>
        </tr>
        </table>
    </div>

            </noscript>
        </div>
           
            <div class="form-submit cbt">
            <input id="submit-button" value="Post Your Answer" tabindex="110" type="submit">
            <a href="#" class="discard-answer dno">discard</a>
    <p class="privacy-policy-agreement">
        By posting your answer, you agree to the 
        <a href="http://stackexchange.com/legal/privacy-policy" target="_blank">privacy policy</a> and 
        <a href="http://stackexchange.com/legal/terms-of-service" target="_blank">terms of service</a>.
    </p>
    <input name="legalLinksShown" value="1" type="hidden">
        </div>
    </form>



        <h2 class="bottom-notice">
                Not the answer you're looking for? 
            Browse other questions tagged <a href="http://stackoverflow.com/questions/tagged/php" class="post-tag" title="show questions tagged 'php'" rel="tag">php</a> <a href="http://stackoverflow.com/questions/tagged/json" class="post-tag" title="show questions tagged 'json'" rel="tag">json</a> <a href="http://stackoverflow.com/questions/tagged/zend-framework2" class="post-tag" title="show questions tagged 'zend-framework2'" rel="tag">zend-framework2</a> <a href="http://stackoverflow.com/questions/tagged/php-5.4" class="post-tag" title="show questions tagged 'php-5.4'" rel="tag">php-5.4</a> <a href="http://stackoverflow.com/questions/tagged/zend-framework-mvc" class="post-tag" title="show questions tagged 'zend-framework-mvc'" rel="tag">zend-framework-mvc</a> 
                or <a href="http://stackoverflow.com/questions/ask">ask your own question</a>.
        </h2>
</div>
</div>
<div id="sidebar" class="show-votes">
        <div class="module question-stats">
        <p class="label-key">tagged</p>
        <div class="tagged"><a href="http://stackoverflow.com/questions/tagged/php" class="post-tag" title="show questions tagged 'php'" rel="tag">php</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">383449</span></span><br>
<a href="http://stackoverflow.com/questions/tagged/json" class="post-tag" title="show questions tagged 'json'" rel="tag">json</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">45108</span></span><br>
<a href="http://stackoverflow.com/questions/tagged/zend-framework2" class="post-tag" title="show questions tagged 'zend-framework2'" rel="tag">zend-framework2</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">1842</span></span><br>
<a href="http://stackoverflow.com/questions/tagged/zend-framework-mvc" class="post-tag" title="show questions tagged 'zend-framework-mvc'" rel="tag">zend-framework-mvc</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">99</span></span><br>
<a href="http://stackoverflow.com/questions/tagged/php-5.4" class="post-tag" title="show questions tagged 'php-5.4'" rel="tag">php-5.4</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">86</span></span><br>
</div>
        <table id="qinfo">
            <tbody><tr>
                <td><p class="label-key">asked</p></td>
                <td style="padding-left:10px"><p class="label-key" title="2012-09-16 22:54:18Z"><b>7 months ago</b></p></td>
            </tr>
            <tr>
                <td><p class="label-key">viewed</p></td>

                <td style="padding-left:10px">
                    <p class="label-key">
                        <b>3875 times</b>
                    </p>
                </td>
            </tr>
            <tr>
                <td><p class="label-key">active</p></td>
                <td style="padding-left:10px"><p class="label-key"><b><a href="http://stackoverflow.com/questions/12451399/how-to-render-zf2-view-within-json-response?lastactivity" class="lastactivity-link" title="2012-10-02 06:24:47Z">7 months ago</a></b></p></td>
            </tr>
        </tbody></table>
    </div>
        
                <script type="text/javascript">
                var scriptSrc = "http://engine.adzerk.net/z/8277/adzerk1_2_4_43,adzerk2_2_17_45,adzerk3_2_4_44?keywords=php,json,zend-framework2,php-5.4,zend-framework-mvc";
                if (document.referrer) {
                    if (/\?/.test(scriptSrc))
                        scriptSrc += "&";
                    else
                        scriptSrc += "?";
                    scriptSrc += "xReferrer=" + document.referrer;
                }
                StackExchange.ready(function () {
                    var z = document.createElement("script");
                    z.type = "text/javascript";
                    z.async = "true";
                    z.src = scriptSrc;
                    var s = document.getElementsByTagName("script")[0];
                    s.parentNode.insertBefore(z, s);
                });
            </script>
        <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk2"><div id="careersadsdoublehigh" style="display:none;"></div><img src="how-to-render-zf2-view-within-json-response_files/i_003.gif" border="0" height="0px" width="0px"><div class="adzerk-vote-controls" style="display:none;"><div class="adzerk-vote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div></div><div class="adzerk-vote-survey" style="display:none;"><form><span>No problem. We won't show you that ad again. Why didn't you like it?</span><ul><li><label><input value="12" name="downvoteReason" type="radio">Uninteresting</label></li><li><label><input value="13" name="downvoteReason" type="radio">Misleading</label></li><li><label><input value="14" name="downvoteReason" type="radio">Offensive</label></li><li><label><input value="15" name="downvoteReason" type="radio">Repetitive</label></li></ul><a href="#" class="adzerk-vote-cancel">Oops! I didn't mean to do this.</a></form></div></div>
    <div id="hireme"> <a href="http://careers.stackoverflow.com/jobs?a=24" class="top" target="_blank"></a> <ul class="jobs"> <li> <a href="http://careers.stackoverflow.com/jobs/32795/senior-badass-net-web-engineer-at-growing-startup-upcounsel?a=CKluYTu" target="_blank" title="Senior Badass .NET Web Engineer at Growing Startup at UpCounsel. Click to learn more."> Senior Badass .NET Web Engineer at Growing Startup<br> <span class="company">UpCounsel</span> <span class="location">San Francisco, CA / relocation</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/34116/tech-lead-resn?a=Ej4JX4A" target="_blank" title="Tech Lead at Resn. Click to learn more."> Tech Lead<br> <span class="company">Resn</span> <span class="location">Amsterdam, Netherlands</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/32865/executive-producer-amazon?a=CPr8Vxe" target="_blank" title="Executive Producer at Amazon. Click to learn more."> Executive Producer<br> <span class="company">Amazon</span> <span class="location">Lake Forest, CA / relocation</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/29854/technical-director-brightcove?a=zgSfF96" target="_blank" title="Technical Director at Brightcove. Click to learn more."> Technical Director<br> <span class="company">Brightcove</span> <span class="location">Boston, MA</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/33056/sr-full-stack-web-developer-demand-media?a=D3qB52o" target="_blank" title="Sr. Full Stack Web Developer at Demand Media. Click to learn more."> Sr. Full Stack Web Developer<br> <span class="company">Demand Media</span> <span class="location">Santa Monica, CA</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/33636/senior-python-developer-digitas?a=DJUKC76" target="_blank" title="Senior Python Developer at Digitas. Click to learn more."> Senior Python Developer<br> <span class="company">Digitas</span> <span class="location">Boston, MA</span> </a> </li> </ul> <img alt="" class="impression" src="how-to-render-zf2-view-within-json-response_files/Ej4JX4A-CPr8Vxe-CKluYTu-zgSfF96-D3qB52o-DJUKC76-24.gif" style="display:none"></div>



      
    
    <div class="module sidebar-linked">
        <h4 id="h-linked">Linked</h4>
        <div class="linked" data-tracker="lq=1">
            <div class="spacer">
<a href="http://stackoverflow.com/q/12743803?lq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">0</div>
</a>
<a href="http://stackoverflow.com/questions/12743803/zend-framework-2-display-a-view-within-a-view?lq=1" class="question-hyperlink">Zend Framework 2 display a view within a view</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/14339744?lq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes default">0</div>
</a>
<a href="http://stackoverflow.com/questions/14339744/multiple-view-strategy-in-zend-2?lq=1" class="question-hyperlink">Multiple view strategy in zend 2</a>
</div>

        </div>
    </div>
    <div class="module sidebar-related">
        <h4 id="h-related">Related</h4>
        <div class="related" data-tracker="rq=1">
            <div class="spacer">
<a href="http://stackoverflow.com/q/8843092?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">2</div>
</a>
<a href="http://stackoverflow.com/questions/8843092/zf2-get-controller-name-into-layout-views?rq=1" class="question-hyperlink">ZF2 - Get controller name into layout/views</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/10426967?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">3</div>
</a>
<a href="http://stackoverflow.com/questions/10426967/how-to-render-a-page-in-zend-framework-2?rq=1" class="question-hyperlink">How to render a page in zend framework 2?</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/12108921?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">0</div>
</a>
<a href="http://stackoverflow.com/questions/12108921/how-to-render-a-different-view-in-controller-action-of-zf2?rq=1" class="question-hyperlink">How to render a different view in controller action of ZF2</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/12612414?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">1</div>
</a>
<a href="http://stackoverflow.com/questions/12612414/zf2-return-json-only-for-ajax-call?rq=1" class="question-hyperlink">ZF2: Return JSON only for Ajax Call</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/12820415?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">1</div>
</a>
<a href="http://stackoverflow.com/questions/12820415/how-to-tell-zf2s-jsonmodel-to-return-text-plain-instead-of-application-json?rq=1" class="question-hyperlink">How to tell ZF2's JsonModel to return text/plain instead of application/json?</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/13256639?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes default">0</div>
</a>
<a href="http://stackoverflow.com/questions/13256639/how-to-avoid-wrapping-response-with-content-when-returning-viewmodel-in-contro?rq=1" class="question-hyperlink">How to avoid wrapping response with “content” when returning ViewModel in controller action?</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/13441471?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes default">1</div>
</a>
<a href="http://stackoverflow.com/questions/13441471/zf2-controllers-forward-plugin-doesnt-work-how-to-make-it-work?rq=1" class="question-hyperlink">ZF2: Controller's Forward plugin doesn't work. How to make it work?</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/13807821?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes default">1</div>
</a>
<a href="http://stackoverflow.com/questions/13807821/zf2-how-to-change-the-error-404-response-page-not-just-template-but-to-set-a?rq=1" class="question-hyperlink">ZF2 - How to change the error/404 response page? Not just template but to set a new ViewModel</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/13978748?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">0</div>
</a>
<a href="http://stackoverflow.com/questions/13978748/zf2-how-to-attach-listener-on-the-event-in-the-module-class?rq=1" class="question-hyperlink">ZF2: How to attach listener on the event in the Module class?</a>
</div>
<div class="spacer">
<a href="http://stackoverflow.com/q/15479262?rq=1" title="Vote score (upvotes - downvotes)">
        <div class="answer-votes answered-accepted default">0</div>
</a>
<a href="http://stackoverflow.com/questions/15479262/zf2-view-rendering?rq=1" class="question-hyperlink">ZF2 view rendering</a>
</div>

        </div>
    </div>
        <div class="everyonelovesstackoverflow" id="adzerk3">
        </div>


</div>

<div id="feed-link">
    <div id="feed-link-text"><a href="http://stackoverflow.com/feeds/question/12451399" title="feed of this question and its answers"><span class="feed-icon"></span>question feed</a></div>
</div>
<script type="text/javascript">
StackExchange.ready(function(){$.get('/posts/12451399/ivc/cbb7');});
</script>
<noscript>
    <div><img src="/posts/12451399/ivc/cbb7" class="dno" alt="" width="0" height="0"></div>
</noscript><div style="display:none" id="prettify-lang">lang-php</div></div>



        </div>
    </div>
        <div id="footer" class="categories">
            <div class="footerwrap">
                <div id="footer-menu">
                    <div class="top-footer-links">
                            <a href="http://stackoverflow.com/about">about</a>
                        <a href="http://stackoverflow.com/faq">faq</a>
                            <a href="http://stackoverflow.com/badges">badges</a>
                        <a href="http://blog.stackexchange.com/?blb=1">blog</a>
                            <a href="http://chat.stackoverflow.com/">chat</a>
                        <a href="http://data.stackexchange.com/">data</a>
                        <a href="http://stackexchange.com/legal">legal</a>
                        <a href="http://stackexchange.com/legal/privacy-policy">privacy policy</a>
                        <a href="http://stackexchange.com/about/hiring">jobs</a>
                        <a href="http://engine.adzerk.net/redirect/0/2776/2751/0/4de3c60f719c4dfcb1a57531c7050090/0">advertising info</a>
                        <a onclick='StackExchange.switchMobile("on", "/questions/12451399/how-to-render-zf2-view-within-json-response")'>mobile</a>
                        <b><a href="http://stackoverflow.com/help">contact us</a></b>
                            <b><a href="http://meta.stackoverflow.com/">feedback</a></b>
                    </div>
                    <div id="footer-sites">
                    <table>
    <tbody><tr>
            <th colspan="3">
                Technology
            </th>
            <th>
                Life / Arts
            </th>
            <th>
                Culture / Recreation
            </th>
            <th>
                Science
            </th>
            <th>
                Other
            </th>
    </tr>
    <tr>
            <td>
                <ol>
                        <li><a href="http://stackoverflow.com/" title="professional and enthusiast programmers">Stack Overflow</a></li>
                        <li><a href="http://serverfault.com/" title="professional system and network administrators">Server Fault</a></li>
                        <li><a href="http://superuser.com/" title="computer enthusiasts and power users">Super User</a></li>
                        <li><a href="http://webapps.stackexchange.com/" title="power users of web applications">Web Applications</a></li>
                        <li><a href="http://askubuntu.com/" title="Ubuntu users and developers">Ask Ubuntu</a></li>
                        <li><a href="http://webmasters.stackexchange.com/" title="pro webmasters">Webmasters</a></li>
                        <li><a href="http://gamedev.stackexchange.com/" title="professional and independent game developers">Game Development</a></li>
                        <li><a href="http://tex.stackexchange.com/" title="users of TeX, LaTeX, ConTeXt, and related typesetting systems">TeX - LaTeX</a></li>
                            </ol></td><td><ol>
                        <li><a href="http://programmers.stackexchange.com/" title="professional programmers interested in conceptual questions about software development">Programmers</a></li>
                        <li><a href="http://unix.stackexchange.com/" title="users of Linux, FreeBSD and other Un*x-like operating systems.">Unix &amp; Linux</a></li>
                        <li><a href="http://apple.stackexchange.com/" title="power users of Apple hardware and software">Ask Different (Apple)</a></li>
                        <li><a href="http://wordpress.stackexchange.com/" title="WordPress developers and administrators">WordPress Answers</a></li>
                        <li><a href="http://gis.stackexchange.com/" title="cartographers, geographers and GIS professionals">Geographic Information Systems</a></li>
                        <li><a href="http://electronics.stackexchange.com/" title="electronics and electrical engineering professionals, students, and enthusiasts">Electrical Engineering</a></li>
                        <li><a href="http://android.stackexchange.com/" title="enthusiasts and power users of the Android operating system">Android Enthusiasts</a></li>
                        <li><a href="http://security.stackexchange.com/" title="IT security professionals">IT Security</a></li>
                            </ol></td><td><ol>
                        <li><a href="http://dba.stackexchange.com/" title="database professionals who wish to improve their database skills and learn from others in the community">Database Administrators</a></li>
                        <li><a href="http://drupal.stackexchange.com/" title="Drupal developers and administrators">Drupal Answers</a></li>
                        <li><a href="http://sharepoint.stackexchange.com/" title="SharePoint enthusiasts">SharePoint</a></li>
                        <li><a href="http://ux.stackexchange.com/" title="user experience researchers and experts">User Experience</a></li>
                        <li><a href="http://mathematica.stackexchange.com/" title="users of Mathematica">Mathematica</a></li>
                    
                        <li>
                            <a href="http://stackexchange.com/sites#technology" class="more">
                                more (14)
                            </a>
                        </li>
                </ol>
            </td>
            <td>
                <ol>
                        <li><a href="http://photo.stackexchange.com/" title="professional, enthusiast and amateur photographers">Photography</a></li>
                        <li><a href="http://scifi.stackexchange.com/" title="science fiction and fantasy enthusiasts">Science Fiction &amp; Fantasy</a></li>
                        <li><a href="http://cooking.stackexchange.com/" title="professional and amateur chefs">Seasoned Advice (cooking)</a></li>
                        <li><a href="http://diy.stackexchange.com/" title="contractors and serious DIYers">Home Improvement</a></li>
                    
                        <li>
                            <a href="http://stackexchange.com/sites#lifearts" class="more">
                                more (13)
                            </a>
                        </li>
                </ol>
            </td>
            <td>
                <ol>
                        <li><a href="http://english.stackexchange.com/" title="linguists, etymologists, and serious English language enthusiasts">English Language &amp; Usage</a></li>
                        <li><a href="http://skeptics.stackexchange.com/" title="scientific skepticism">Skeptics</a></li>
                        <li><a href="http://judaism.stackexchange.com/" title="those who base their lives on Jewish law and tradition and anyone interested in learning more">Mi Yodeya (Judaism)</a></li>
                        <li><a href="http://travel.stackexchange.com/" title="road warriors and seasoned travelers">Travel</a></li>
                        <li><a href="http://gaming.stackexchange.com/" title="passionate videogamers on all platforms">Arqade (gaming)</a></li>
                        <li><a href="http://bicycles.stackexchange.com/" title="people who build and repair bicycles, people who train cycling, or commute on bicycles">Bicycles</a></li>
                        <li><a href="http://rpg.stackexchange.com/" title="gamemasters and players of tabletop, paper-and-pencil role-playing games">Role-playing Games</a></li>
                    
                        <li>
                            <a href="http://stackexchange.com/sites#culturerecreation" class="more">
                                more (22)
                            </a>
                        </li>
                </ol>
            </td>
            <td>
                <ol>
                        <li><a href="http://math.stackexchange.com/" title="people studying math at any level and professionals in related fields">Mathematics</a></li>
                        <li><a href="http://stats.stackexchange.com/" title="statisticians, data analysts, data miners and data visualization experts">Cross Validated (stats)</a></li>
                        <li><a href="http://cstheory.stackexchange.com/" title="theoretical computer scientists and researchers in related fields">Theoretical Computer Science</a></li>
                        <li><a href="http://physics.stackexchange.com/" title="active researchers, academics and students of physics">Physics</a></li>
                    
                        <li>
                            <a href="http://stackexchange.com/sites#science" class="more">
                                more (7)
                            </a>
                        </li>
                </ol>
            </td>
            <td>
                <ol>
                        <li><a href="http://stackapps.com/" title="apps, scripts, and development with the Stack Exchange API">Stack Apps</a></li>
                        <li><a href="http://meta.stackoverflow.com/" title="meta-discussion of the Stack Exchange family of Q&amp;A websites">Meta Stack Overflow</a></li>
                        <li><a href="http://area51.stackexchange.com/" title="proposing new sites in the Stack Exchange network">Area 51</a></li>
                        <li><a href="http://careers.stackoverflow.com/">Stack Overflow Careers</a></li>
                    
                </ol>
            </td>
    </tr>
</tbody></table>
                    </div>
                </div>

                <div id="copyright">
                    site design / logo © 2013 stack exchange inc; 
                    user contributions licensed under <a href="http://creativecommons.org/licenses/by-sa/3.0/" rel="license">cc-wiki</a> 
                    with <a href="http://blog.stackoverflow.com/2009/06/attribution-required/" rel="license">attribution required</a>
                </div>
                <div id="footer-flair">
                    <a href="http://creativecommons.org/licenses/by-sa/3.0/" class="cc-wiki-link"></a>
                </div>
                <div id="svnrev">
                    rev 2013.5.1.673
                </div>
            
            </div>
        <iframe id="global-auth-frame" style="display:none" src="how-to-render-zf2-view-within-json-response_files/read.html"></iframe></div>
    <noscript>
        <div id="noscript-warning">Stack Overflow works best with JavaScript enabled<img src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno"></div>
    </noscript>
    <script type="text/javascript">var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-5620270-1']);
        _gaq.push(['_setCustomVar', 1, 'tags', '|php|json|zend-framework2|php-5.4|zend-framework-mvc|']); 
_gaq.push(['_trackPageview']);
    var _qevents = _qevents || [];
    (function(){
        var s=document.getElementsByTagName('script')[0];
        var ga=document.createElement('script');
        ga.type='text/javascript';
        ga.async=true;
        ga.src='http://www.google-analytics.com/ga.js';
        s.parentNode.insertBefore(ga,s);
        var sc=document.createElement('script');
        sc.type='text/javascript';
        sc.async=true;
        sc.src='http://edge.quantserve.com/quant.js'; 
        s.parentNode.insertBefore(sc,s);
    })();
    </script>
    <script type="text/javascript">
        _qevents.push({ qacct: "p-c1rF4kxgLUzNc" });
    </script>        
    

</body></html>